<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use app\modules\Product\models\Product;
use app\models\Supplier;
use yii\widgets\LinkPager;
use kartik\datetime\DateTimePicker;
use kartik\date\DatePicker;

$this->title = "列表模式, 所有订单";
?>
<style type="text/css">
	table tr td p font{ color:#666;margin-right:10px;}
	.style-10 {color: #ccc;}
	.style-11 {color: #ccc;}
	.style-12 {color: #ccc;}
	.style-10 td p font{color: #ccc;}
	.style-11 td p font{color: #ccc;}
	.style-12 td p font{color: #ccc;}
	.style-relation {background: rgba(166, 100, 166,0.1);}
</style>
<div class="order-index">
    <h2 class="page-header"><small><?= Html::encode("列表模式: 所有订单") ?></small>
    	<p class="pull-right">
    		<a href="/order/word/order-excel?<?=http_build_query(\Yii::$app->request->queryParams)?>" target="_blank" class="btn btn-success down">下载订单列表</a>
			<?php if($show == 1) { ?>
				<a href="/order/order/my-index?&show=2" class="btn btn-info">显示占位取消订单</a>
			<?php }else{ ?>
				<a href="/order/order/my-index?&show=1" class="btn btn-warning">隐藏占位取消订单</a>
			<?php } ?>
    	</p>
	</h2>

    <!-- ?PHP Pjax::begin(['id'=>'order', 'timeout'=>3000, 'linkSelector'=>'#order a[data-sort], #order a[data-page]']);? -->

    <?php  echo $this->render('_mysearch', ['model' => $searchModel]); ?>

	<table class="table " style="margin-top:15px;font-size:12px;">
		<thead><tr> <th>基本信息</th> <th>舱房信息</th> <th>联系人</th>  <th>金额</th> <th>状态</th><th></th> </tr></thead>
		<tbody>
		<?php foreach($dataProvider->getModels() as $m){ ?>
			<tr class="style-<?= $m->status ?> <?= ($m->isRelation > 1) ? 'style-relation' : '' ?>">
				<td colspan=6 >
 					<b><?= $m->product->name ?> &nbsp;&nbsp;<?= $m->product->go_time ?></b>
				</td>
			</tr>
			<tr class="style-<?= $m->status ?> <?= ($m->isRelation > 1) ? 'style-relation' : '' ?>">

				<td class="col-md-2" style="border:0px;">
					<p><font>销售</font> <?= isset($m->member->id) ? $m->member->profile->name : '(未设置)'  ?> </p>
					<p><font>创建时间</font> <?= Yii::$app->formatter->asDate($m->created_at)?></p>
					<p><font>更新时间</font> <?= Yii::$app->formatter->asRelativeTime($m->updated_at)?></p>
					<p><font>订单号</font> <?= $m->code ?></p>
				</td>
				<td class="col-md-4" style="border:0px;">
					<?php 
						if($m->product->stock == 1){ // 国内航线
							if($m->status <10){
								
								$orderRoom = $m->room;
							}else{
							
								$orderRoom = $m->roomAll;
							}	
							foreach($orderRoom as $room){ // orderRoom
								echo "<p>";
            					echo $room->productRoom->room->name;
            					echo '  '.$room->productRoom->room->code;
            					echo '  '.$room->productRoom->people .'人间&nbsp;&nbsp;';
								//echo " /&nbsp;&nbsp;&nbsp;已录入".$m->getRoomCustomer($room->id)->count() ." 人";
								echo "房号: ".$room->code;
								echo " &nbsp;&nbsp;&nbsp;";
        //	return $this->hasMany(OrderRoomCustomer::className(), ['order_room_id' => 'id'])->onCondition(['status'=>1]);
									//$customers = $m->getRoomCustomer($room->id)->all();
									$customers = $room->orderRoomCustomer;
									if($customers){
										foreach($customers as $c){
											if($c->orderCustomer && $c->orderCustomer->status == 1){
											//if($c->orderCustomer->status == 1){
											echo ' / <a class="show-customer" href="/order/customer/get-message?id='. $c->orderCustomer->id.'"  data-toggle="modal" data-target=".log-modal" >'.$c->orderCustomer->first_name.$c->orderCustomer->last_name.'</a>' ; 
											if($c->orderCustomer->complete == 1){
												echo '&nbsp;<i class="fa  fa-check-square"></i>';
											}
											}
										}	
									}
								echo "</p>";
							}
						}elseif($m->product->stock == 2){ // 海外航线
							$t = json_decode($m->product->rooms, true);	

							if($m->status <10){
								
								$orderRoom = $m->room;
							}else{
							
								$orderRoom = $m->roomAll;
							}	

							foreach($orderRoom as $room){ // orderRoom
								echo "<p>";
            					print_r($t[$room->room_id]['name']);
            					echo "&nbsp;&nbsp; 房号: ".$room->code;
								echo " &nbsp;&nbsp;&nbsp;";
									$customers = $m->getRoomCustomer($room->id)->all();
									//$customers = $room->orderRoomCustomer;
									if($customers){
										foreach($customers as $c){
											//if($c->orderCustomer->status == 1){
											//echo ' / '.$c->orderCustomer->first_name . $c->orderCustomer->last_name ; 
											echo ' / <a class="show-customer" href="/order/customer/get-message?id='. $c->orderCustomer->id.'"  data-toggle="modal" data-target=".log-modal" >'.$c->orderCustomer->first_name.$c->orderCustomer->last_name.'</a>' ; 
											if($c->orderCustomer->complete == 1){
												echo '&nbsp;<i class="fa  fa-check-square"></i>';
											}
										}	
									}
								echo "</p>";
							}
						}
					?>
				</td>
				<td class="col-md-2" style="border:0px;">
					<p><font>联系人</font> <?= $m->username ?></p>
					<p><font>电  话</font>  <?= $m->phone ?></p>
					<p><font>公  司</font>  <?= $m->company ?></p>
					<p><font>备  注</font>  <?= $m->remark ?></p>
				</td>

				<td class="col-md-2" style="border:0px;">
					<p><font>舱房金额</font><?= Yii::$app->formatter->asCurrency($m->total_sale) ?></p>
					<p>
						<font>优惠金额</font><?= Yii::$app->formatter->asCurrency($m->sale_price) ?>
						<?= ($m->check == 1 && $m->sale_price>0) ? '<i class="text-danger fa fa-check-circle" title="优惠审核标识"></i>' : '' ?>
					</p>	
					<p><font>附加费</font> <?= Yii::$app->formatter->asCurrency($m->priceSum) ?></p>
					<p><font>总金额</font> <b style="color:<?= ($m->status == 10) ? '#CCC;' : '#FF4500;' ?>"><?= Yii::$app->formatter->asCurrency($m->price) ?></b></p>
					<p><font>实收</font> <?= Yii::$app->formatter->asCurrency($m->financeSuccess) ?></p>
				</td>
				<td class="col-md-1" style="border:0px;">
					<p><?= Yii::$app->params['order_status'][$m->status] ?></p>
					<p><?= empty($m->assign_id) ? '未分团' :  $m->assign->name ?></p>
					<p><?= ($m->isRelation > 1) ? '<i class="fa fa-link" title="关联单标识"></i>' : '' ?></p>

					<p><a class="fa fa-calendar-check-o"  href="/order/order/log?id=<?= $m->id?>" title='操作记录' data-toggle='modal' data-target='.log-modal'></a></p>
					<?php if($m->is_merge == 2){ ?>
						<p><i class="fa fa-handshake-o" <?= empty($m->merge_id) ? '' : "style=color:red;" ?> title='拼房标识' ></i></p>
					<?php } ?>
				</td>

				<td style="border:0px;">
					<?php if($m->status < 10){ ?>
						<p>&nbsp;</p><p><a href="/order/order/view?id=<?= $m->id?>" target="_blank">详情</a></p>
						<p><a class="create_excel" href="javascript:;" title="生成确认单" data-id="<?=$m->id?>">确认单</a></p>
						<p><a href="/finance/finance/quick-create?order_id=<?= $m->id ?>">财务</a></p>
					<?php }elseif($m->status == 10){ ?>

						<p>&nbsp;</p>
						<p>
							<?= Html::a('恢复', ['/order/order/recovery', 'id' => $m->id], [
									'class' => 'btn btn-info btn-sm',
									'data' => [
										'confirm' => '确定恢复该条订单吗?',
										'method' => 'post',
									],
								]) ?>
						</p>
					<?php } ?>
				</td>
			</tr>
		<?php } ?>
		</tbody>
	</table>

	<?= LinkPager::widget(['pagination' => $dataProvider->pagination]); ?>
    <!-- ?php Pjax::end(); ? -->

</div>


<div class="modal fade log-modal"   role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document" style="width:740px;">
      <div class="modal-content">
	 		<!-- 嵌入内容 --> 
	  </div>
  </div>
</div>


<div class="modal fade config-modal"  role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document" style="width:740px;">
      <div class="modal-content">
      	<form id="config-form" method="get" action="/order/word/down-excel" style="margin: 20px;">
	 		<input type="hidden" name="id" id="order-id" value="">
	 		<!-- <label></label>
           	<div class="form-group">
           		</?php 
           		echo '<label>盖章确认截止时间</label>';
				echo DateTimePicker::widget([
					'id' => 'begin_date',
				    'name' => 'begin_date', 
				    'value' => '',
				    'options' => ['placeholder' => '选择日期和时间'],
				    'pluginOptions' => [
				        'format' => 'yyyy-mm-dd hh:ii',
                        'autoclose'=>true,
                        'todayHighlight' => true
				    ]
				]);
				?>
           	</div> -->
           	<div class="form-group">
           		<?php 
           		echo '<label>付全款截止时间</label>';
				echo DatePicker::widget([
					'id' => 'end_date',
				    'name' => 'end_date', 
				    'value' => '',
				    'options' => ['placeholder' => '选择日期'],
				    'pluginOptions' => [
				        'format' => 'yyyy-mm-dd',
                        'autoclose'=>true,
                        'todayHighlight' => true
				    ]
				]);
				?>
           	</div>
           	<div class="form-group">
           		<?php 
           		echo '<label>模板</label>';
				echo Html::radioList('tem_sheet','1',\yii::$app->params['config_sheet'],['class'=>'form-control']);
				?>
           	</div>
        </form>
        <div class="text-center" style="margin:20px;">
        	<button class="btn btn-success" id="create">生成Excel</button>
       	</div> 	
	  </div>
  </div>
</div>


<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
// 清除模态框缓存
$(".log-modal").on("hidden.bs.modal", function() {
	$(this).removeData("bs.modal");
});

$(".config-modal").on("hidden.bs.modal", function() {
    $(this).removeData("bs.modal");
});

$(".create_excel").click(function(){
     $('.config-modal').modal('show');
     var order_id = $(this).attr("data-id");
     $("#order-id").val(order_id);
});

$("#create").click(function(){
	//var begin_date = $("#begin_date").val();
	var end_date = $("#end_date").val();
	if(!end_date){
		alert("时间不能为空！");
		return false;
	}
    $("#config-form").submit();
});

// 显示用户详细信息 
/*
$(".show-customer").hover(
	function(){
		var id = $(this).data('id');
		//alert(id);
		$.get('/order/customer/get-message', { id: id},
			function(data){
				if(data){
					console.log(data);
				}
			});
	},function(){
	
	}
); */

</script>
<?php app\components\JsBlock::end() ?>   
